/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import HibernateUtil.HibernateUtil;
import Interface.InterfaceReferencias;
import Pohos.Login;
import Pohos.Referencias;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Duvan11
 */
public class DaoReferencias implements InterfaceReferencias{
    Session session=null;
    @Override
    public boolean register(Referencias referencias)
    {
       try{
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction transaction = session.beginTransaction();
            session.save(referencias);
            transaction.commit();
       }catch(HibernateException e){
            System.out.println(e.getMessage());
            session.getTransaction().rollback();
       }
       finally{
            if(session !=null){
               session.close(); 
            }
       }
       return true;
    }

    @Override
    public boolean update(Referencias referencias) throws Exception {
       session = HibernateUtil.getSessionFactory().openSession();
       Transaction transaction = session.beginTransaction();
       session.save(referencias);
       transaction.commit();
       session.close();
       return true;
    }    

    @Override
    public List<Referencias> MostrarReferencias(){
       session = null;
        List<Referencias> lista = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from Referencias");
            lista = (List<Referencias>) query.list();
        }catch(HibernateException e){
            System.out.println(e.getMessage());
        }
        finally{
            if(session !=null){
               session.close();
            }        
        }        
        return lista;  
    }
}
